﻿create PROCEDURE Sync.SP_CommitteeBankAccountDepositAttachments_ApplyChanges
@RawValues XML 
AS 
BEGIN
	 
		DECLARE @CommitteeBankAccountDepositAttachments_Temp TABLE 
		(
			CommitteeBankAccountDepositAttachmentID UniqueIdentifier,
			[CommitteeBankAccountDepositID] uniqueidentifier,
				[Attachment] uniqueidentifier,
			IsDeleted BIT, 
			LastUpdatedDate DATETIME
		)
		
	   INSERT INTO @CommitteeBankAccountDepositAttachments_Temp 
	   SELECT	Tbl.Col.value('CommitteeBankAccountDepositAttachmentID[1]','uniqueidentifier') CommitteeBankAccountDepositAttachmentID,
                Tbl.Col.value('CommitteeBankAccountDepositID[1]', 'uniqueidentifier') [CommitteeBankAccountDepositID],
Tbl.Col.value('Attachment[1]', 'uniqueidentifier') [Attachment],
				Tbl.Col.value('IsDeleted[1]', 'bit') IsDeleted,
				Tbl.Col.value('LastUpdatedDate[1]','DateTime') LastUpdatedDate
	   FROM   @RawValues.nodes('/DocumentElement/CommitteeBankAccountDepositAttachments') Tbl(Col)
----------------------------------------------------------------------------------------

-- Insert ------------------------------------------------------------------------------

	INSERT INTO [dbo].[CommitteeBankAccountDepositAttachments] ([CommitteeBankAccountDepositAttachmentID],[CommitteeBankAccountDepositID],[Attachment])
	SELECT t.[CommitteeBankAccountDepositAttachmentID],t.[CommitteeBankAccountDepositID],t.[Attachment]
	FROM  @CommitteeBankAccountDepositAttachments_Temp t
	WHERE t.IsDeleted = 0
    AND NOT EXISTS
    (
        SELECT 1 FROM sync.CommitteeBankAccountDepositAttachments_Tracking s
        WHERE t.CommitteeBankAccountDepositAttachmentID = s.CommitteeBankAccountDepositAttachmentID
    )
	-- put conflicts here.
	
-- Update -------------------------------------------------------------------------------
     
	UPDATE [dbo].[CommitteeBankAccountDepositAttachments]
	SET [CommitteeBankAccountDepositAttachments].[CommitteeBankAccountDepositID] = t.[CommitteeBankAccountDepositID],[CommitteeBankAccountDepositAttachments].[Attachment] = t.[Attachment] 
	FROM [dbo].[CommitteeBankAccountDepositAttachments] s JOIN @CommitteeBankAccountDepositAttachments_Temp t 
    ON t.CommitteeBankAccountDepositAttachmentID = s.CommitteeBankAccountDepositAttachmentID
	WHERE	t.IsDeleted = 0 
	AND		t.LastUpdatedDate IS NOT NULL
	AND		t.LastUpdatedDate >= 
				 isnull((SELECT TOP 1 LastUpdatedDate 
				  FROM sync.CommitteeBankAccountDepositAttachments_Tracking r
				  WHERE t.CommitteeBankAccountDepositAttachmentID = r.CommitteeBankAccountDepositAttachmentID)
                  ,t.LastUpdatedDate)
	-----------------------------------------------------------------------------------------

-- Delete -------------------------------------------------------------------------------

	DELETE [dbo].[CommitteeBankAccountDepositAttachments]
    FROM @CommitteeBankAccountDepositAttachments_Temp t JOIN [dbo].[CommitteeBankAccountDepositAttachments] s
    ON t.CommitteeBankAccountDepositAttachmentID = s.CommitteeBankAccountDepositAttachmentID
	WHERE t.IsDeleted = 1

-----------------------------------------------------------------------------------------
	
END











